package Reflect;

import domain.Person;

import java.lang.reflect.Constructor;

/**
 * @Author: ylc
 * @Description:
 * @Date Created in 2022-04-19 15:24
 */
public class ReflectDemo2 {
    public static void main(String[] args) throws Exception{
        Class personCls = Person.class;
        /* 获取构造方法们
         * Constructor<?>[] getConstructors()
         * Constructor<T> getConstructor(类<?>... parameterTypes)

         * Constructor<T> getDeclaredConstructor(类<?>... parameterTypes)
         * Constructor<?>[] getDeclaredConstructors()
         */
        //1.通过构造器创建对象
        Constructor constructor = personCls.getConstructor(String.class);
        Object b = constructor.newInstance("b");
        System.out.println(b);

        System.out.println("-------------------");

        Constructor constructor1 = personCls.getConstructor();
        System.out.println(constructor1);
        //2.通过构造器创建对象
        Object person = constructor1.newInstance();
        System.out.println(person);

        //3.空参构造简化方式
        Object o = personCls.newInstance();
        System.out.println(o);

    }
}
